//
//  APUserDataManager.m
//  AqualPolo
//
//  Created by dong on 2024/5/6.
//

#import "APUserDataManager.h"
#import "APDiscoveryDataManager.h"
#import "APLoginManager.h"
#import "APVideoDataManager.h"

@implementation APUserDataManager
+ (APUserDataManager *)shareInstance{
    static APUserDataManager *shareId = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareId = [[self alloc] init];
    });
    return shareId;
}

- (APBroadcasterModel *)searchUserByUserId:(NSString *)userId{
    if (self.users.count > 0) {
        for (APBroadcasterModel *model in self.users) {
            if ([model.userId isEqualToString:userId]) {
                return model;
            }
        }
    }
    return nil;
}

- (void)unfollow:(NSString *)userId result:(APUserResultBlock)callBack{
    if (userId == nil) {
        return;
    }
    [APDataManager postJsonWithUrlString:unfriend timeoutInterval:0 parameters:@{@"followUserId":userId} result:^(NSDictionary * _Nullable res) {
        [self updateUser:userId isFriend:false];
        callBack(true,@"Unfollow successfully");
    } failBlock:^(NSString * _Nullable errorMsg) {
        callBack(false,errorMsg);
    }];
}

- (void)follow:(NSString *)userId result:(APUserResultBlock)callBack{
    [APDataManager postJsonWithUrlString:addFriend timeoutInterval:0 parameters:@{@"followUserId":userId} result:^(NSDictionary * _Nullable res) {
        [self updateUser:userId isFriend:true];
        callBack(true,@"Follow successfully.");
    } failBlock:^(NSString * _Nullable errorMsg) {
        callBack(false,errorMsg);
    }];
}


- (void)blockUser:(NSString *)userId result:(APUserResultBlock)callBack{
   NSDictionary *parameters = @{@"broadcasterId": userId,@"complainCategory": @"Block"};
    [APDataManager postJsonWithUrlString:insertBlockRecord timeoutInterval:0 parameters:parameters result:^(NSDictionary * _Nullable res) {
        [self unfollow:userId result:^(_Bool success, NSString * _Nonnull msg) {
            [[APDiscoveryDataManager shareInstance] blockUserWorks:userId];
            [[APVideoDataManager shareInstance] blockUserVideos:userId];
            callBack(true,@"Blocked successfully");
        }];
    } failBlock:^(NSString * _Nullable errorMsg) {
        callBack(false,errorMsg);
    }];
}

- (void)unBlockUser:(NSString *)userId result:(APUserResultBlock)callBack{
    [APDataManager postJsonWithUrlString:removeBlock timeoutInterval:0 parameters:@{@"blockUserId": userId} result:^(NSDictionary * _Nullable res) {
        [[APDiscoveryDataManager shareInstance] unBlockUserWorks:userId];
        [[APVideoDataManager shareInstance] unBlockUserVideos:userId];
        callBack(true,@"");
     } failBlock:^(NSString * _Nullable errorMsg) {
         callBack(false,errorMsg);
     }];
}




- (void)updateUser:(NSString *)userId isFriend:(BOOL)isFriend{
    for (APBroadcasterModel *model in self.users) {
        if ([model.userId isEqualToString:userId]) {
            model.isFriend = isFriend;
        }
    }
    NSArray *res = [self.users yy_modelToJSONObject];
    if (res) {
        [self saveInfo:res toFile:usersFile];
    }
}

- (void)loadUsers:(LoadUserResultBlock)callBack{
    if (self.users && self.users.count > 0) {
        callBack(self.users);
        return;
    }
   NSArray *users = [self readFileDataAtPath:usersFile];
    if (users && users.count > 0) {
        NSMutableArray *result = [NSMutableArray array];
        for (NSDictionary *dict in users) {
            APBroadcasterModel *model = [APBroadcasterModel yy_modelWithDictionary:dict];
            [result addObject:model];
        }
        self.users = result;
        callBack(result);
        return;
    }
    
    self.users = @[];
    NSDictionary *parameters = @{
         @"category": @"Popular", // 分类固定传Popular
         @"isPageMode": @true,
         @"isRemoteImageUrl": @true, // 必须为true 否则返回的头像链接需要拼接
         @"limit": @20,
         @"page": @1,
         @"tag": @"All"
       };
    [APDataManager postJsonWithUrlString:broadcasterWall timeoutInterval:20 parameters:parameters result:^(NSDictionary * _Nullable res) {
        NSMutableArray *users = [NSMutableArray array];
        NSArray *result = res[@"data"];
        if (result && [result isKindOfClass:[NSArray class]]) {
            for (NSDictionary *dict in result) {
                APBroadcasterModel *userModel = [APBroadcasterModel yy_modelWithDictionary:dict];
                [users addObject:userModel];
            }
            
        }
        self.users = users;
        [self saveInfo:result toFile:usersFile];
        callBack(users);
    } failBlock:^(NSString * _Nullable errorMsg) {
        callBack(@[]);
    }];
}

- (void)clearData{
    [BaseDataManager removeFile:usersFile];
    [BaseDataManager removeFile:images_path];
}
@end
